package cn.wolfcode.luowowo.mgrsite.task;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StatisVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.common.util.RedisKey;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * spring定时器
 * 用于将Redis数据持久化到MySQL中
 */
//@Component
public class RedisDataPersistenceTask {

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    // Cron表达式是一个字符串，字符串以5或6个空格隔开，分为6或7个域，每一个域代表一个含义，Cron有如下两种语法格式：
    // （1） Seconds Minutes Hours DayofMonth Month DayofWeek Year
    //（2）Seconds Minutes Hours DayofMonth Month DayofWeek
    //       0       0      2        1       *       ?        *

    //定时任务标签
    //cron :定时任务计划, 指定时间用于持久化存储
    @Scheduled(cron = "0/10 * * * * ?")
    public void completeTask() {
        //1. 查询所有vo
        List<StrategyStatisVO> list = strategyStatisVOService.listVO(RedisKey.STRATEGY_STATIS_VO.join("*"));
        //2. update到数据库中
        for (StrategyStatisVO vo : list) {
            StatisVO statisVO = new StatisVO();
            BeanUtils.copyProperties(vo, statisVO);
            strategyDetailService.updateStatisVo(statisVO);
        }
    }
}
